package com.changgou.goods.controller;

import com.changgou.goods.pojo.Para;
import com.changgou.goods.service.ParaService;
import com.github.pagehelper.PageInfo;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/para")
@CrossOrigin
public class ParaController {
    @Autowired
    private ParaService paraService;

    @PostMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Para>> findPage(@RequestBody(required = false) Para para, @PathVariable int page, @PathVariable int size) {
        //执行搜索
        PageInfo<Para> pageInfo = paraService.findPage(para, page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @GetMapping(value = "/search/{page}/{size}")
    public Result<PageInfo<Para>> findPage(@PathVariable int page, @PathVariable int size) {
        //分页查询
        PageInfo<Para> pageInfo = paraService.findPage(page, size);
        return new Result<>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    @PostMapping(value = "/search")
    public Result<List<Para>> findList(@RequestBody(required = false) Para para) {
        List<Para> list = paraService.findList(para);
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @DeleteMapping(value = "/{id}")
    public Result<Integer> delete(@PathVariable Integer id) {
        paraService.delete(id);
        return new Result<>(true, StatusCode.OK, "删除成功");
    }

    @PutMapping(value = "/{id}")
    public Result<Integer> update(@RequestBody Para para, @PathVariable Integer id) {
        //设置主键值
        para.setId(id);
        //修改数据
        paraService.update(para);
        return new Result<>(true, StatusCode.OK, "修改成功", id);
    }

    @PostMapping
    public Result<Integer> add(@RequestBody Para para) {
        paraService.add(para);
        return new Result<>(true, StatusCode.OK, "添加成功", para.getId());
    }

    @GetMapping("/{id}")
    public Result<Para> findById(@PathVariable Integer id) {
        //根据ID查询
        Para para = paraService.findById(id);
        return new Result<>(true, StatusCode.OK, "查询成功", para);
    }

    @GetMapping
    public Result<Para> findAll() {
        List<Para> list = paraService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", list);
    }

    @GetMapping(value = "/category/{id}")
    public Result<List<Para>> getByCategoryId(@PathVariable(value = "id") Integer id) {
        //根据分类ID查询对应的参数信息
        List<Para> paras = paraService.findByCategoryId(id);
        return new Result<>(true, StatusCode.OK, "查询分类对应的品牌成功！", paras);
    }
}
